<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_Disciplina extends MY_Controller {

	function __construct()
	{
		parent::__construct();
                $this->load->helper('html');
                $this->load->helper('url');
	}

	function index(){

            $this->load->view('disciplina/v_busca_disciplina');
	            
	}

        function cadastrarDisciplina(){

            $this->load->view('disciplina/v_nova_disciplina');

        }

        function detalharDisciplina(){

            extract($_POST);

            $disciplina = $this->em->find('models\disciplina',$id);

            $data['id']         = $id;
            $data['tipo']       = ($disciplina->getDisciplinaTipo() == 1) ? 'Obrigat&oacute;ria' : 'Opcional';
            $data['titulo']     = $disciplina->getDisciplinaDescResumida();
            $data['status']     = ($disciplina->getDisciplinaStatus() == 1) ? 'Ativa' : 'Inativa';
            $data['descricao']  = $disciplina->getDisciplinaDesc();

            $this->load->view('disciplina/p_info_disciplina',$data);;

        }

        function gravarDisciplina(){
            extract($_POST);
            
            $disciplina = new models\disciplina;

            $disciplina->setDisciplinaDesc($descDisc);
            $disciplina->setDisciplinaDescResumida($descDiscResumida);
            $disciplina->setDisciplinaTipo($tipoDisc);
            $disciplina->setDisciplinaStatus(1);

            $this->em->persist($disciplina);
            $this->em->flush();

            $linhaRetorno  ='<tr class="gradeA" style="font-size: 10px ; border: #000 1px solid " id="tr_'.$disciplina->getDisciplinaCod().'">
                                <td><div align="center" id="titulo_'.$disciplina->getDisciplinaCod().'">'.$disciplina->getDisciplinaDescResumida().'</div></td>
                                <td><div align="center" id="desc_'.$disciplina->getDisciplinaCod().'">'. $disciplina->getDisciplinaDesc().'</div></td>
                                <td>
                                <div align="center">
                                    <a href="#" id="lnkview">
                                        <img alt="" src="'. base_url() . 'assets/images/ico_view.png" border="0" title="Visualizar" onclick="detalharDisciplina('.$disciplina->getDisciplinaCod().')" />
                                    </a>
                                </div>
                            </td>
                            <td>
                                <div align="center">
                                    <a href="#" id="lnkupdate">
                                        <img alt="" src="'.base_url() . 'assets/images/refresh.png" border="0" title="Atualizar" onclick="updateDisciplina('.$disciplina->getDisciplinaCod().')"/>
                                    </a>
                                </div>
                            </td>
                            <td>
                                <div align="center">
                                    <a href="#" id="lnkdelete">
                                        <img alt="" src="'. base_url() . 'assets/images/delete.png" border="0" title="Excluir" width="20" height="20" onclick="excluirDisciplina('.$disciplina->getDisciplinaCod().')"/>
                                    </a>
                                </div>
                            </td>
                         </tr>';

            echo TRUE.'@'.$linhaRetorno;
        }

        function listarDisciplina(){

            $data['disciplinaList'] = $this->em->createQuery('SELECT d FROM models\disciplina d WHERE d.disciplinaStatus = 1');

            $this->load->view('disciplina/v_lista_disciplina',$data);

        }

        function buscaDisciplina(){

            $this->load->view('disciplina/v_busca_disciplina');
        }

        function buscaDisciplinaLista(){

            extract($_POST);
            $descDiscResumida = "'%". strtoupper($descDiscResumida)."%'";
           
            $data['disciplinaList'] = $this->em->createQuery('SELECT d FROM models\disciplina d WHERE d.disciplinaStatus = 1 AND upper(d.disciplinaDescResumida) LIKE    '.$descDiscResumida );
            
            $this->load->view('disciplina/i_lista_disciplina',$data);

        }

        function updateDisciplina(){
            extract($_POST);

            $disciplina = $this->em->find('models\disciplina',$id);

            $data['id']         = $id;
            $data['tipo']       = $disciplina->getDisciplinaTipo();
            $data['titulo']     = $disciplina->getDisciplinaDescResumida();
            $data['descricao']  = $disciplina->getDisciplinaDesc();
   
            $this->load->view('disciplina/v_update_disciplina',$data);
        }

        function executaUpdate(){

            extract($_POST);

            $disciplina = $this->em->find('models\disciplina',$id);

            $disciplina->setDisciplinaDesc($descricao);
            $disciplina->setDisciplinaDescResumida($titulo);
            $disciplina->setDisciplinaTipo($tipo);

            $this->em->merge($disciplina);

            $this->em->flush();
            
            echo TRUE;

        }

        function executaExclusao(){

            extract($_POST);

            $disciplina = $this->em->find('models\disciplina',$id);

            $disciplina->setDisciplinaStatus(0);

            $this->em->merge($disciplina);

            $this->em->flush();

            echo TRUE.'@'.$disciplina->getDisciplinaCod();

        }

        function buscaPorNome(){

            $descDiscResumida = "'%". strtoupper($_POST['param'])."%'";

            $dados = $this->em->createQuery('SELECT d FROM models\disciplina d WHERE d.disciplinaStatus = 1 AND upper(d.disciplinaDescResumida) LIKE    '.$descDiscResumida );

            foreach ($dados->getResult() as $key=>$value){

                echo "$key|$value\n";

            }

        }

        function testeview(){
            $this->load->view('teste');
        }
	
}